//
//  DLTabbarController.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/22.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLTabbarController.h"
#import "DLBaseNavController.h"
#import "DLMachineVC.h"
#import "DLInvestVC.h"
#import "DLInformationVC.h"
#import "DLMineVC.h"
#import "DLTabBar.h"
#import "DLRiskEvaluationVC.h"

#import "DLSettingVC.h"
#import "DLFalseMineVC.h"



@interface DLTabbarController ()<UITabBarControllerDelegate>

@end

@implementation DLTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.delegate = self;
    [self layoutSubControls];
    
    [self setValue:[[DLTabBar alloc] init] forKey:@"tabBar"];
    [DLTabBar appearance].translucent = NO;
    // Do any additional setup after loading the view.
}


- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    
    DLBaseNavController *nav = (DLBaseNavController *) viewController;
    
    if ([[nav.viewControllers objectAtIndex:0] isKindOfClass:[DLFalseMineVC class]] || [[nav.viewControllers objectAtIndex:0] isKindOfClass:[DLInvestVC class]]) {
//    if ([[nav.viewControllers objectAtIndex:0] isKindOfClass:[DLMineVC class]] || [[nav.viewControllers objectAtIndex:0] isKindOfClass:[DLInvestVC class]]) {
//        if ([[nav.viewControllers objectAtIndex:0] isKindOfClass:[DLMineVC class]]) {
        if ([[nav.viewControllers objectAtIndex:0] isKindOfClass:[DLFalseMineVC class]]) {
            //点击的是“我的”
            if (!kuserManager.isLogin) {
                
                [kuserManager userLogin];
                return NO;
            } else {
                
                if (kuserManager.isTokenAvailable == NO) {
                    //token是否可用
                    kuserManager.isLogin = NO;
                    kuserManager.isRequestUserInfo = YES;
                    [kuserManager userLogin];
                    return NO;
                } else {
                    kuserManager.isRequestUserInfo = YES;
                    return YES;
                }
            }
        } else {
            //点击的是“投资组合”
            if (!kuserManager.isLogin) {
                
                [kuserManager userLogin];
                return NO;
            } else {
                if (kuserManager.userInfoModel.isRiskEvaluation == YES) {
                    
                    return YES;
                } else {
                    DLRiskEvaluationVC *vc = [DLRiskEvaluationVC new];
                    DLBaseNavController *loginNav = [[DLBaseNavController alloc] initWithRootViewController:vc];
                    [self presentViewController:loginNav animated:YES completion:^{
                        
                    }];
                    return NO;
                }
            }
        }
    } else {

        return YES;
    }
}



- (void) layoutSubControls {
    
//    DLMachineVC *machineVC = [DLMachineVC new];
//    DLInvestVC *investVC = [DLInvestVC new];
//    DLInformationVC *informationVC = [DLInformationVC new];
//    DLMineVC *mineVC = [DLMineVC new];
//
//    NSArray *controllers = @[machineVC, investVC, informationVC, mineVC];
//    NSArray *titlesArr = @[@"首页", @"投资组合", @"资讯", @"我的"];
//    NSArray *normalImgArr = @[@"tab_machine_normal", @"tab_invest_normal", @"tab_info_normal", @"tab_mine_normal"];
//    NSArray *selectedImgArr = @[@"tab_machine_selected", @"tab_invest_selected", @"tab_info_selected", @"tab_mine_selected"];
    
    
    DLMachineVC *machineVC = [DLMachineVC new];
    DLInformationVC *informationVC = [DLInformationVC new];
    DLFalseMineVC *settingVC = [DLFalseMineVC new];
    
    NSArray *controllers = @[machineVC, informationVC, settingVC];
    NSArray *titlesArr = @[@"智语", @"智讯", @"我的"];
    NSArray *normalImgArr = @[@"tab_machine_normal", @"tab_info_normal", @"tab_mine_normal"];
    NSArray *selectedImgArr = @[@"tab_machine_selected", @"tab_info_selected", @"tab_mine_selected"];
    
    
    NSMutableArray *vcArr = [NSMutableArray array];
    for (NSInteger i = 0; i < controllers.count; i++) {
        
        UIViewController *controller = [controllers objectAtIndex:i];
        controller = [self viewControllerWithController:controller tabTitle:titlesArr[i] image:normalImgArr[i] selectImage:selectedImgArr[i]];
        DLBaseNavController *navController = [[DLBaseNavController alloc] initWithRootViewController:controller];
        [vcArr addObject:navController];
    }
    
    
    self.viewControllers = vcArr;
    self.selectedIndex = 0;
}


- (UIViewController *)viewControllerWithController:(UIViewController *)viewController tabTitle:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage {
    
    viewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.title = title;
    
    
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:kDLBtnNormalBgColor,
                                                        NSFontAttributeName:[UIFont systemFontOfSize:12]}
                                             forState:UIControlStateSelected];
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor],
                                                        NSFontAttributeName:[UIFont systemFontOfSize:12]}
                                             forState:UIControlStateNormal];
    return viewController;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
